package com.balance.anole.erp;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;

public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {

    public ApplicationWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) {
        super(configurer);
    }

    public ActionBarAdvisor createActionBarAdvisor(IActionBarConfigurer configurer) {
        return new ApplicationActionBarAdvisor(configurer);
    }
    
    public void preWindowOpen() {
		IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
		Display display = configurer.getWindow().getWorkbench().getDisplay();
		
		// Get the resolution
		Rectangle pDisplayBounds = display.getPrimaryMonitor().getBounds();
		configurer.setInitialSize(new Point(pDisplayBounds.width, pDisplayBounds.height));
		configurer.setShowCoolBar(true);
		configurer.setShowStatusLine(true);
		configurer.setShowProgressIndicator(true);
		configurer.setShellStyle(SWT.TITLE | SWT.CLOSE | SWT.MIN | SWT.MAX);
	}

	public void postWindowCreate() {
		super.postWindowCreate();
		centerWorkbenchWindow();
	}

	private void centerWorkbenchWindow() {
		IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
		Display display = configurer.getWindow().getWorkbench().getDisplay();
		Shell shell = configurer.getWindow().getShell();

		// Get the resolution
		Rectangle pDisplayBounds = display.getPrimaryMonitor().getBounds();

		int nMinWidth = 950;
		int nMinHeight = pDisplayBounds.height - 50;
		// This formulae calculate the shell's Left ant Top
		int nLeft = (pDisplayBounds.width - nMinWidth) / 2;
		int nTop = (pDisplayBounds.height - nMinHeight) / 2;

		// Set shell bounds,
		shell.setBounds(nLeft, nTop, nMinWidth, nMinHeight);
	}
}
